package com.zjsru.oneDay202204;

/**
 * @Author: likew
 * @Date: 2022/4/5
 *
 * 二进制表示中质数个计算置位
 *
 * 输入：left = 6, right = 10
 * 输出：4
 * 解释：
 * 6 -> 110 (2 个计算置位，2 是质数)
 * 7 -> 111 (3 个计算置位，3 是质数)
 * 9 -> 1001 (2 个计算置位，2 是质数)
 * 10-> 1010 (2 个计算置位，2 是质数)
 * 共计 4 个计算置位为质数的数字。
 */
public class countPrimeSetBits {
    static boolean[] hash = new boolean[40];
    static {
        int[] nums = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
        for (int x : nums) hash[x] = true;
    }
    public int countPrimeSetBits(int left, int right) {
        int res = 0;
        for (int i = left; i <= right ; i++) {
            int x = i, cnt = 0;
            while(x != 0 && ++cnt>0) x-=(x & -x);
            if(hash[cnt]){
                res++;
            }
        }
        return res;
    }
    
    public static void main(String[] args) {
        countPrimeSetBits countPrimeSetBits = new countPrimeSetBits();
        int left = 6, right = 10;
        System.out.println(countPrimeSetBits.countPrimeSetBits(left, right));
    }
}
